// pages/personal-integral/personal-integral.js

import api from '../../utils/request/api'
const util = require('../../utils/util.js');
import {
  userIntegral
} from '../../utils/request/apiurl'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: "",
    uid: "",
    // uid: "da3bdf61cc1a409cafe573ea389c0e09",
    integral: 0,
   isRequest: false,
   searchGood: {
		page: 1,
		size: 10,
		pages: 0
   },
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取我的积分
	
    api.post(userIntegral.integral, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid
    }).then(res => {
      // console.log(res)
      this.setData({
        integral: res || 0
      })
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
    this.getIntegralList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	var userInfo=wx.getStorageSync('userInfo')
	var userData=wx.getStorageSync('Info')
	this.setData({
		uid:userInfo.uid,
		userData:userData
	})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
  	wx.showNavigationBarLoading() //在标题栏中显示加载
	this.setData({
		dataList: [],
		searchGood: {
		  page: 1,
		  size: 10,
		  pages:0,
		}
	})
  	this.onLoad()
  	setTimeout(function() {
  		wx.hideNavigationBarLoading() //完成停止加载
  		wx.stopPullDownRefresh() //停止下拉刷新
  	}, 1500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	  console.log(this.data.searchGood.pages,this.data.isRequest,this.data.searchGood.page)
	if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
	this.setData({
	  searchGood: {
	    page: this.data.searchGood.page + 1,
	    size: 10,
	    pages: this.data.searchGood.pages
	  }
	})
		this.getIntegralList()
	}
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 说明
  toMore() {
    wx.navigateTo({
      url: '../integralMore/integralMore?type=integral'
    })
  },
  //获取积分流水列表
  getIntegralList() {
    api.post(userIntegral.list, {
      uid: this.data.uid||wx.getStorageSync('userInfo').uid,
      type: 2,
      page: this.data.searchGood.page,
      size: this.data.searchGood.size
    }).then(res => {
      // console.log(res)
      let list = res.rows
      list.map(a => {
        a.recordTime = util.getLocalTime(a.recordTime)
        return a
      })
	  let pages = res.total / this.data.searchGood.size
      this.setData({
        dataList: [...this.data.dataList, ...list],
        searchGood: {
          pages:pages,
          page: this.data.searchGood.page,
          size: this.data.searchGood.size,
        }
      })
      // console.log(this.data.dataList)
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  }
})